package cc.vv.btpartyjob.partyservice.fragment

import android.support.v4.widget.SwipeRefreshLayout
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.view.BTLoadMoreView
import cc.vv.btpartyjob.jnimodule.binder.BaseBinder
import cc.vv.btpartyjob.partyservice.R
import cc.vv.btpartyjob.partyservice.adapter.MineWishAdapter
import cc.vv.btpartyjob.partyservice.delegate.MineWishFragmentDelegate
import cc.vv.btpartyjob.partyservice.module.bean.MineWishClaimBean
import cc.vv.btpartyjob.partyservice.module.resp.MainWishClaimResponseObj
import cc.vv.btpartyjob.partyservice.server.DataPartyServer
import cc.vv.mvp.binder.BaseDataBinder
import cc.vv.mvp.fragment.BaseFragmentMVP
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 * 我的认领--心愿fragment
 * Created by Chen.qi on 2018/10/27.
 */
class MineClaimWishFragment : BaseFragmentMVP<MineWishFragmentDelegate>()
        , SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.OnItemClickListener,
        BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener {
    /**
     * 适配器
     */
    private var mAdapter: MineWishAdapter? = null

    /**
     * 当前页数
     */
    private var pageNo: Int = 1
    /**
     * 每页条数
     */
    private val pageSize: Int = 10

    /**
     * 集合数据
     */
    private var mDataList: ArrayList<MineWishClaimBean>? = null

    private var whetherRefresh = false

    override fun initData() {
        if (mAdapter == null) {
            mDataList = ArrayList()
            mAdapter = MineWishAdapter(R.layout.item_mine_wish)
            viewDelegate?.setRecyclerViewAdapter(mAdapter)
            mAdapter?.setLoadMoreView(BTLoadMoreView())
            viewDelegate?.setAdapterData(mAdapter, mDataList)
        }
    }

    override fun onVisibilityChanged(visible: Boolean) {
        super.onVisibilityChanged(visible)
        if (visible && !whetherRefresh) {
            pageNo = 1
            DataPartyServer.getMainWishClaimList(pageNo, pageSize, true)
            whetherRefresh = true
        }
    }

    override fun bindEvenListener() {
        mAdapter?.onItemClickListener = this
        mAdapter?.onItemChildClickListener = this
        viewDelegate?.bindRefreshListener(this)
        viewDelegate?.bindLoadMore(mAdapter, this)
    }

    override fun getDelegateClass(): Class<MineWishFragmentDelegate> {
        return MineWishFragmentDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        if (whetherRefresh) {
            when (obj) {
                is MainWishClaimResponseObj -> {
                    viewDelegate?.stopRefreshing()
                    if (null == obj.data) {
                        return
                    }
                    if (1 == pageNo) {
                        mDataList?.clear()
                        if (null != obj.data.records) {
                            mDataList!!.addAll(obj.data.records)
                        }
                        viewDelegate?.setAdapterData(mAdapter, mDataList)
                        if (mDataList!!.size < 10) {
                            mAdapter?.loadMoreEnd(true)
                            mAdapter?.loadMoreEnd()
                        }
                    } else {
                        if (mAdapter?.data?.size!! >= obj.data.total) {
                            //数据全部加载完毕
                            mAdapter?.loadMoreEnd(true)
                            mAdapter?.loadMoreEnd()
                        } else {
                            //成功获取更多数据
                            if (null != obj.data.records) {
                                mDataList!!.addAll(obj.data.records)
                            }
                            mAdapter?.setNewData(mDataList)
                            //加载数据
                            mAdapter?.disableLoadMoreIfNotFullPage()
                            mAdapter?.loadMoreComplete()
                        }
                    }
                }
            }
        }

    }

    override fun getBadCode(obj: BaseResponseObj<*>?) {
        super.getBadCode(obj)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
        mAdapter?.loadMoreComplete()
        mAdapter?.loadMoreFail()
    }

    override fun onHttpFailure(url: String?, exceptionStr: String?) {
        super.onHttpFailure(url, exceptionStr)
        if (1 == pageNo) {
            viewDelegate?.stopRefreshing()
        }
    }


    override fun onRefresh() {
        mAdapter?.setEnableLoadMore(false)//这里的作用是防止下拉刷新的时候还可以上拉加载
        pageNo = 1
        DataPartyServer.getMainWishClaimList(pageNo, pageSize, false)
    }

    override fun onLoadMoreRequested() {
        if (mDataList!!.size < 10) {
            mAdapter?.loadMoreComplete()
            mAdapter?.loadMoreEnd(true)
            return
        }
        pageNo++
        DataPartyServer.getMainWishClaimList(pageNo, pageSize, false)
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        if (position < mDataList!!.size) {
            mDataList!![position].selecOpen = !mDataList!![position].selecOpen
            mAdapter?.notifyDataSetChanged()
        }
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
        //微心愿详情
//        val intent = Intent()
//        intent.putExtra(IntentServiceKey.INTENT_KEY_TINY_DETAIL_ID, mDataList!![position].id)
//        intent.setClass(context, TinyWishDetailActivity::class.java)
//        startActivity(intent)
    }
}